package com.kuang.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kuang.pojo.Product;
import com.kuang.pojo.Sale;
import com.kuang.service.ProductServiceImpl;
import com.kuang.service.SaleServiceImpl;
import com.sun.deploy.net.HttpRequest;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
public class saleController02 {
    @Autowired
    private ProductServiceImpl productService;
    @Autowired
    private SaleServiceImpl saleServicel;
    @RequestMapping("/a1")
    public String a1( String name) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(name);
        String msg= productService.productbyname(name);
        String msg1="库存内一共还有";
        String msg2="件商品";
        String msg3=msg1+msg+msg2;
        String s = mapper.writeValueAsString(msg3);
        return s;
    }
    @RequestMapping("/a2")
    public String a2(String name,String number) throws JsonProcessingException {
        String msg4="商品库存不足，请重新输入数据，或者添加库存";
        ObjectMapper mapper = new ObjectMapper();
        String s = mapper.writeValueAsString(msg4);
        String msg= productService.productbyname(name);
        double v = Double.parseDouble(msg);
        double v1 = Double.parseDouble(number);
        if (v<v1){

            return s;
        }
        return "";
    }
    @RequestMapping("/a3")
    public String a3(String name) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(name);
        String msg= productService.productbyname(name);
        String msg1="库存内一共还有";
        String msg2="件商品";
        String msg3=msg1+msg+msg2;
        String s = mapper.writeValueAsString(msg3);
        return s;
    }
    //验证进货的时候  库存表里的这个商品是否存在
    @RequestMapping("/a4")
    public String a4(String name,HttpServletRequest request){
        ObjectMapper mapper = new ObjectMapper();
        HttpSession session = request.getSession();
        List<Product> product = productService.getProduct();
        for (Product product1 : product) {
            if (product1.getName().equals(name)){
                String price = product1.getJinjia();
                return "该商品已经在库存中，且进价是"+price;
            }
        }
             return null;

    }
//退货表  根据输入的商品名称 查询销售表里的数据
    @RequestMapping("/a5")
    public String a5(String name) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();


        Sale sale = saleServicel.getnumberByname(name);
        String number = sale.getNumber();
        String s = mapper.writeValueAsString(number);
        return "已经销售了该商品:"+s+"件";
    }


}
